package com.slipper.util;

import com.slipper.exception.CommonException;
import static com.slipper.constants.Constants.Database.*;

public class DatabaseUtil {
    private static String type = "";

    public static void setType(String type) {
        if (Tool.isBlank(type)) return;

        type = type.toLowerCase();
        if (type.contains("microsoft sql server")) {
            type = SQLSERVER;
        }
        DatabaseUtil.type = type;
    }

    public static String getType() {
        return type;
    }

    public static String wrapSymbol(String value) {
        String symbol = getSymbol(getType());
        return symbol + value + symbol;
    }

    public static String wrapSymbol(String dbType, String value) {
        String symbol = getSymbol(dbType);
        return symbol + value + symbol;
    }

    public static String getSymbol(String dbType) {
        return switch (dbType.toLowerCase()) {
            case MYSQL -> "`";
            case ORACLE, POSTGRESQL, SQLSERVER -> "\"";
            default -> throw new CommonException("不支持此数据库类型【" + dbType + "】！");
        };
    }
}
